﻿
Imports System.Runtime.CompilerServices
Imports osi.root.connector
Imports osi.root.formation
Imports osi.root.utils
Imports osi.root.procedure

Public Module _instances
    Public ReadOnly named_istrkeyvt As unique_strong_map(Of String, istrkeyvt)
    Public ReadOnly named_ifs As unique_strong_map(Of String, ifs)

    Sub New()
        named_istrkeyvt = New unique_strong_map(Of String, istrkeyvt)()
    End Sub

    <Extension()> Public Function [stop](ByVal ss() As istrkeyvt) As event_comb
        Dim ecs() As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  If isemptyarray(ss) Then
                                      Return False
                                  Else
                                      ReDim ecs(array_size(ss) - 1)
                                      For i As Int32 = 0 To array_size(ss) - 1
                                          If ss(i) Is Nothing Then
                                              Return False
                                          Else
                                              ecs(i) = ss(i).stop()
                                          End If
                                      Next
                                      Return waitfor(ecs) AndAlso
                                             goto_next()
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return ecs.end_result() AndAlso
                                         goto_end()
                              End Function)
    End Function

    <Extension()> Public Function [stop](ByVal ss As vector(Of istrkeyvt)) As event_comb
        Return [stop](+ss)
    End Function

    <Extension()> Public Function [stop](ByVal ss As unique_strong_map(Of String, istrkeyvt)) As event_comb
        Dim v As vector(Of istrkeyvt) = Nothing
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  If ss Is Nothing Then
                                      Return False
                                  Else
                                      v = New vector(Of istrkeyvt)()
                                      assert(ss.foreach(Sub(ByRef n As String, ByRef i As istrkeyvt)
                                                            v.emplace_back(i)
                                                        End Sub))
                                      ec = v.stop()
                                      Return waitfor(ec) AndAlso
                                             goto_next()
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         goto_end()
                              End Function)
    End Function

    Public Function stop_named_istrkeyvt() As event_comb
        Return named_istrkeyvt.stop()
    End Function
End Module
